# Kurz, Wurthmann, Gross #

library(haven)
library(ggplot2)
library(dplyr)
library(stargazer)
library(ggpubr)
library(margins)

getwd()
setwd()

#### Recoding before subsetting ####

# Age

# data_nat$v4
# table(data_nat$v4)
# 
# data_nat$age <- data_nat$v4
# data_nat$age_rec <- ifelse(data_nat$age <31,1,
#                            ifelse(data_nat$age >=31 & data_nat$age <=59,2,
#                                                 ifelse(data_nat$age >=60,3,NA)))
# 
# 
# 
# 
# # Representation # 
# 
# data_nat$v14k
# data_nat$youngrep <- data_nat$v14k
# data_nat$youngrep[data_nat$youngrep<1] <- NA
# 
# data_nat$v14l
# data_nat$oldrep <- data_nat$v14l
# data_nat$oldrep[data_nat$oldrep<1] <- NA
# 
# 
# # Voting #
# 
# data_nat$voting <- NA
# data_nat$v10
# data_nat$voting[data_nat$v10 ==2 ] <- 1 #CSU + CDU
# data_nat$voting[data_nat$v10 ==1] <- 2 #SPD
# data_nat$voting[data_nat$v10 ==4  ] <- 3 #FDP
# data_nat$voting[data_nat$v10 ==3   ] <- 4 #GREENS
# data_nat$voting[data_nat$v10 ==6 ] <- 5 #LEFT
# data_nat$voting[data_nat$v10 ==5   ] <- 6 #AfD
# data_nat$voting[data_nat$v10 ==7  ] <- 7 #Free Voters
# data_nat$voting[data_nat$v10 ==801   ] <- 8 #Others
# data_nat$voting[data_nat$v10 ==802  ] <- 9 #Nonvoting
# 
# # Age Importance Level ###
# 
# data_nat$agelocal <- data_nat$v38f
# data_nat$agestate <- data_nat$v39f
# data_nat$agenat <- data_nat$v40f
# 
# 
# 
# 
# variables <- c("agelocal", "agestate", "agenat")
# 
# for (variable in variables) {
#   data_nat[[variable]][data_nat[[variable]] < 1] <- NA
# }
# 
# 
# # Sex Importance level # 
# 
# data_nat$sexlocal <- data_nat$v38a
# data_nat$sexstate <- data_nat$v39a
# data_nat$sexnat <- data_nat$v40a
# 
# 
# variables <- c("sexlocal", "sexstate", "sexnat")
# 
# # Loop
# 
# for (variable in variables) {
#   data_nat[[variable]][data_nat[[variable]] < 1] <- NA
# }
# 
# 
# 
# 
# # Job Importance level # 
# 
# data_nat$joblocal <- data_nat$v38b
# data_nat$jobstate <- data_nat$v39b
# data_nat$jobnat <- data_nat$v40b
# 
# 
# variables <- c("joblocal", "jobstate", "jobnat")
# 
# 
# for (variable in variables) {
#   data_nat[[variable]][data_nat[[variable]] < 1] <- NA
# }
# 
# 
# # Migration Importance level # 
# 
# data_nat$miglocal <- data_nat$v38d
# data_nat$migstate <- data_nat$v39d
# data_nat$mignat <- data_nat$v40d
# 
# variables <- c("miglocal", "migstate", "mignat")
# 
# # Loop
# 
# for (variable in variables) {
#   data_nat[[variable]][data_nat[[variable]] < 1] <- NA
# }
# 
# # Sexuality Importance Level #
# 
# 
# data_nat$sexualitylocal <- data_nat$v38c
# data_nat$sexualitystate <- data_nat$v39c
# data_nat$sexualitynat <- data_nat$v40c
# 
# 
# variables <- c("sexualitylocal", "sexualitystate", "sexualitynat")
# 
# # Loop
# 
# for (variable in variables) {
#   data_nat[[variable]][data_nat[[variable]] < 1] <- NA
# }
# 
# # Living Importance Level # 
# 
# data_nat$livinglocal <- data_nat$v38e
# data_nat$livingstate <- data_nat$v39e
# data_nat$livingnat <- data_nat$v40e
# 
# 
# variables <- c("livinglocal", "livingstate", "livingnat")
# 
# # Loop
# 
# for (variable in variables) {
#   data_nat[[variable]][data_nat[[variable]] < 1] <- NA
# }
# 
# 
# 
# # Sex 
# 
# data_nat$v2
# data_nat$sex <- data_nat$v2
# table(data_nat$sex)
# data_nat$sex[data_nat$sex>2] <- NA
# 
# # Progressive-Conservative
# 
# data_nat$v37
# data_nat$lib_con <- data_nat$v37
# data_nat$lib_con[data_nat$lib_con>12] <- NA
# data_nat$lib_con[data_nat$lib_con<1] <- NA
# 
# 
# # Urban-Rural
# 
# data_nat$v53
# data_nat$urbanrural <- ifelse(data_nat$v53 ==1,1,
#                               ifelse(data_nat$v53 ==2,2,  
#                                      ifelse(data_nat$v53 ==3,3, 
#                                             ifelse(data_nat$v53 ==4,4, 
#                                                    ifelse(data_nat$v53 >=5,5,NA)))))
# 
# # CLass
# data_nat$class<- ifelse(data_nat$v52 <=2,1,
#                       ifelse(data_nat$v53 ==3,2, 
#                                     ifelse(data_nat$v53 ==4,3, 
#                                            ifelse(data_nat$v53 >=5,4,NA))))
# 
# 
# # Education 
# table(data_nat$v5)
# 
# data_nat$education <- ifelse(data_nat$v5 <=2,1,
#                              ifelse(data_nat$v5 ==3,2,
#                                     ifelse(data_nat$v5 >=4,3,NA)))
# 
# # Representation satisfaction
# data_nat$v18
# data_nat$representsatis <- data_nat$v18
# data_nat$representsatis[data_nat$representsatis<1] <- NA
# table(data_nat$representsatis)
# 
# # Satisfaciton with Democracy
# 
# data_nat$v8
# data_nat$demsatis <- data_nat$v8
# data_nat$demsatis[data_nat$demsatis<1] <- NA
# 
# # Political Interest
# data_nat$v7
# data_nat$polint <- data_nat$v7
# data_nat$polint[data_nat$polint<1] <- NA
# 
# #Blueworker
# table(data_nat$v59)
# data_nat$v59[data_nat$v59<1] <- NA
# 
# data_nat$blueworker <- ifelse(data_nat$v59==1,1,0)
# table(data_nat$blueworker)
# 
# # Further variables 
# 
# data_nat$female_rep <- data_nat$v14a
# data_nat$female_rep[data_nat$female_rep<1] <- NA
# 
# data_nat$male_rep <- data_nat$v14b
# data_nat$male_rep[data_nat$male_rep<1] <- NA
# 
# data_nat$bluecollar <- data_nat$v14d 
# data_nat$bluecollar[data_nat$bluecollar<1] <- NA

#### Create Subset Nation ####


# subset_nation <- data_nat[c("age", "age_rec", "sex", "lib_con", "urbanrural", "education",
#                             "representsatis", "polint","demsatis","blueworker",
#                             "youngrep","oldrep","female_rep","male_rep", "bluecollar",
#                             "agelocal", "agestate", "agenat")]
# 
# subset_nation <- na.omit(subset_nation)

#### Load Data ####
# getwd()
# setwd("")
subset_nation <- readRDS("subset_nation.rds")



#### Subset Nation Boxplots ####

library(ggplot2)
library(reshape2)

# subset_boxplots <- data_nat[c("agelocal", "agestate", "agenat", 
#                               "sexlocal", "sexstate", "sexnat", 
#                               "joblocal", "jobstate", "jobnat", 
#                               "miglocal", "migstate", "mignat", 
#                               "sexualitylocal", "sexualitystate", "sexualitynat", 
#                               "livinglocal", "livingstate", "livingnat", "youngrep","oldrep", "age_rec")]

subset_boxplots <- readRDS("subset_boxplots.rds")

ageboxplots <- subset_boxplots[c("youngrep","oldrep", "age_rec")]

subset_boxplots <- subset_boxplots %>% select(-youngrep, -oldrep, -age_rec)

data_long <- melt(subset_boxplots, measure.vars = c("agelocal", "agestate", "agenat", 
                                             "sexlocal", "sexstate", "sexnat", 
                                             "joblocal", "jobstate", "jobnat", 
                                             "miglocal", "migstate", "mignat", 
                                             "sexualitylocal", "sexualitystate", "sexualitynat", 
                                             "livinglocal", "livingstate", "livingnat"))

data_long$Category <- gsub("(.*)(local|state|nat)$", "\\2", data_long$variable)
data_long$Category <- factor(data_long$Category, levels = c("local", "state", "nat"))
data_long$Group <- gsub("(.*)(local|state|nat)$", "\\1", data_long$variable)
data_long$Category <- factor(data_long$Category, levels = c("local", "state", "nat"))

boxplots_IMP <- ggplot(data_long, aes(x = Group, y = value, fill = Category)) + 
  geom_boxplot(position = position_dodge(width = 0.75)) +
  stat_summary(fun = mean, geom = "point", shape = 20, size = 3, color = "black", 
               position = position_dodge(width = 0.75)) +
  labs(x = "", y = "", fill = "Level") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  scale_fill_manual(values = c("local" = "darkgrey", "state" = "lightgrey", "nat" = "white"),
                    labels = c("local" = "Local", "state" = "State", "nat" = "Nation"),
                    limits = c("nat", "state", "local")) + # reorder legend only
  theme_classic()+
theme(text=element_text(size=12, family="serif",color = "black"),
      legend.text = element_text(color = "black", size = 12),
      axis.text=element_text(size=12, family="serif",color = "black"),
      axis.title=element_text(size=12,family="serif",color = "black"),
      axis.text.y   = element_text(family = "serif", size=12,color = "black"),
      axis.text.x   = element_text(family = "serif", size=12,color = "black"))+
  scale_y_continuous (breaks = c(1,2,3,4,5),
                      labels=c("not at all important","rather not important", 
                               " partly/partly", 
                               "rather important","very important"))+
  scale_x_discrete(labels = c("age" = "Age", 
                              "sex" = "Sex", 
                              "job" = "Occupation", 
                              "mig" = "Migration Background", 
                              "sexuality" = "Sexuality", 
                              "living" = "Place of Residence"))+
  theme(legend.position = "bottom") +coord_flip()



boxplots_IMP + font("xlab", size = 14, color = "black")

ggsave(filename="Fig2.pdf",
         width=12,
         height=6,
         device='pdf',
         dpi=1200)


#### boxplots young and old representatives #####
data_long_dv <- melt(ageboxplots, measure.vars = c("youngrep", "oldrep"))

ggplot(data_long_dv, aes(x = variable, y = value)) + 
  geom_boxplot(fill = "white", color = "black") +
  stat_summary(fun = mean, geom = "point", shape = 17, size = 3.5, color = "black") +
  labs(x = "Age of representative", y = "") +
  theme_minimal() +
  scale_x_discrete(labels = c("youngrep" = "30 years and younger", 
                              "oldrep" = "60 years and older")) +
  scale_y_continuous(breaks = c(1, 2, 3,4,5,6,7,8,9,10,11),
                     label = c("—5\nNot at all\ndesirable", "—4", "—3","—2","—1",
                               "0","+1","+2","+3","+4","+5\nCompletely\ndesirable"))+
  coord_flip() + theme_classic()+
  theme(text=element_text(size=12, family="serif",color = "black"),
        legend.text = element_text(color = "black", size = 12),
        axis.text=element_text(size=12, family="serif",color = "black"),
        axis.title=element_text(size=15,family="serif",color = "black"),
        axis.text.y   = element_text(family = "serif", size=12,color = "black"),
        axis.text.x   = element_text(family = "serif", size=12,color = "black"))
  
ggsave(filename="Fig1.pdf",
       width=10,
       height=4,
       device='pdf',
       dpi=1200)


#### boxplots young and old representatives BY AGE GROUPS #####
data_long_dv <- melt(ageboxplots, measure.vars = c("youngrep", "oldrep"))

ggplot(data_long_dv, aes(x = variable, y = value, fill = factor(age_rec))) + 
  geom_boxplot(color = "black", position = position_dodge(width = 0.8)) +  # Boxplots nebeneinander
  stat_summary(fun = mean, geom = "point", shape = 17, size = 3.5, color = "black", 
               position = position_dodge(width = 0.8)) +  # Mittelwerte pro Altersgruppe
  labs(x = "Age of representative", y = "", fill = "Age group") +
  theme_minimal() +
  scale_x_discrete(labels = c("youngrep" = "30 years and younger", 
                              "oldrep" = "60 years and older")) +
  scale_y_continuous(breaks = c(1, 2, 3,4,5,6,7,8,9,10,11),
                     labels = c("—5\nNot at all\ndesirable", "—4", "—3","—2","—1",
                                "0","+1","+2","+3","+4","+5\nCompletely\ndesirable")) +
  coord_flip() + 
  theme_classic() +
  theme(text = element_text(size = 12, family = "serif", color = "black"),
        legend.text = element_text(color = "black", size = 12),
        axis.text = element_text(size = 12, family = "serif", color = "black"),
        axis.title = element_text(size = 15, family = "serif", color = "black"),
        axis.text.y = element_text(family = "serif", size = 12, color = "black"),
        axis.text.x = element_text(family = "serif", size = 12, color = "black"))+
  guides(fill = guide_legend(reverse = TRUE)) +
  scale_fill_manual(labels = c("<31" ,"31—59" ,"60+"),
                    values=c("white", "#676767",  "darkgrey"))

ggsave(filename="Fig4.pdf",
       width=10,
       height=4,
       device='pdf',
       dpi=1200)


# Rename subset 

subset <- subset_nation

subset$age_rec <- as.factor(subset$age_rec)
subset$age_rec <- relevel(subset$age_rec, ref = "2")


youngrep_model <- lm(formula = youngrep ~ factor(age_rec) + sex + blueworker+ education + 
                     urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)

oldrep_model <- lm(formula = oldrep ~ factor(age_rec) + sex+ blueworker + education + 
                       urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)

femalerep_model <- lm(formula = female_rep ~ factor(age_rec) + sex + blueworker+ education + 
                     urbanrural + polint + lib_con + demsatis + representsatis, data=subset)

malerep_model <- lm(formula = male_rep ~ factor(age_rec) + sex + blueworker+ education + 
                        urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)

bluecollarrep_model <- lm(formula = bluecollar ~ factor(age_rec) + sex+ blueworker + education + 
                      urbanrural + polint + lib_con + demsatis + representsatis, data=subset)

#### Linear model plots main models ####

library(jtools)


represplot <- plot_coefs(youngrep_model,oldrep_model,
                         ci_level = 0.95,
                        model.names = c("Young\nrepresentative", "Old\nrepresentative"), 
                        colors=c("#000000", "#b7b5b9"),
                        legend.title = "",
                        order.variable = "original", point.shape = TRUE, 
                        point.size = 2.5,
                        line.size = 1.2,coefs= c("18—30 years (ref.= 31—59)" = "factor(age_rec)1",
                                                 "60+ years (ref.= 31—59)" = "factor(age_rec)3",
                                                 "Female" = "sex", 
                                                 "Blue Collar Worker" = "blueworker",
                                                 "Education" = "education", 
                                                 "Urban—Rural" = "urbanrural",
                                                 "Political Interest" = "polint", 
                                                 "Ideology: Liberal—Conservative" = "lib_con",
                                                 "Satisfaction: Democracy" = "demsatis",
                                                 "Satisfaction: Representation" = "representsatis")) +
  theme(text=element_text(size=12, family="serif",color = "black"),legend.text = element_text(color = "black", size = 12),
        axis.text=element_text(size=12, family="serif",color = "black"),
        axis.title=element_text(size=12,family="serif",color = "black"),
        axis.text.y   = element_text(family = "serif", size=12,color = "black"),
        axis.text.x   = element_text(family = "serif", size=12,color = "black")) +  
  scale_x_continuous(breaks=seq(-1.5, 1.5, 0.5), limits=c(-1.5,2), labels=c("—1.5","—1","—0.5","0","0.5","1.0","1.5"))+ xlab("")

represplot


represplot + font("xlab", size = 12, color = "black") + theme(axis.text = element_text(colour = "black"))+
  theme(legend.position="bottom") 

ggsave(filename="Fig5.pdf",
       width=11,
       height=7,
       device='pdf',
       dpi=800)


### VIF ####
library(olsrr)
ols_vif_tol(youngrep_model)

#### Tabular presentation of the regressions ####
stargazer(youngrep_model, oldrep_model, femalerep_model, malerep_model, bluecollarrep_model,
          type= "text", star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001), notes = c("+ p<0.1; * p<0.05; ** p<0.01; *** p<0.001"), notes.append=FALSE)


# Age and Level #


age_local_model <- lm(formula = agelocal ~ factor(age_rec) + sex + education + 
                     urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)


age_state_model <- lm(formula = agestate ~ factor(age_rec) + sex + education + 
                        urbanrural + polint + lib_con + demsatis + representsatis, data=subset)

age_nation_model <- lm(formula = agenat ~ factor(age_rec) + sex + education + 
                        urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)


stargazer(age_local_model, age_state_model, age_nation_model,
          type= "text", star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001), notes = c("+ p<0.1; * p<0.05; ** p<0.01; *** p<0.001"), notes.append=FALSE)

ageplotties <- plot_coefs(age_local_model, age_state_model, age_nation_model, ci_level = 0.95,
                         model.names = c("Local level", "State level", "National level"), 
                         colors=c("#000000", "#676767",  "darkgrey"),
                         legend.title = "",
                         order.variable = "original", point.shape = FALSE, 
                         point.size = 2,
                         line.size = 1.2,coefs= c("18—30 years (ref.= 31—59)" = "factor(age_rec)1",
                                                  "60+ years (ref.= 31—59)" = "factor(age_rec)3",
                                                  "Female" = "sex", 
                                                  "Education" = "education", 
                                                  "Urban—Rural" = "urbanrural",
                                                  "Political Interest" = "polint", 
                                                  "Ideology: Liberal—Conservative" = "lib_con",
                                                  "Satisfaction: Democracy" = "demsatis",
                                                  "Satisfaction: Representation" = "representsatis")) +
  theme(text=element_text(size=12, family="serif",color = "black"),legend.text = element_text(color = "black", size = 12),
        axis.text=element_text(size=12, family="serif",color = "black"),
        axis.title=element_text(size=12,family="serif",color = "black"),
        axis.text.y   = element_text(family = "serif", size=12,color = "black"),
        axis.text.x   = element_text(family = "serif", size=12,color = "black")) +  
  scale_x_continuous(breaks=seq(0, 1.0, 0.5), limits=c(-0.4,1.0))+ xlab("")

ageplotties


ageplotties + font("xlab", size = 12, color = "black") + theme(axis.text = element_text(colour = "black"))+
  theme(legend.position="bottom") 

ggsave(filename="Fig3.pdf",
       width=8,
       height=5,
       device='pdf',
       dpi=800)



#### Descriptives####

#### Young Descriptive Rep #####

subset_young_desc <- subset %>%
  group_by(age_rec, youngrep) %>%
  summarise(count = n()) %>%
  group_by(age_rec) %>%
  mutate(percent_young = count / sum(count))

# Plot 
youngrepresentation_age <- ggplot(subset_young_desc, aes(x = youngrep, y = percent_young, fill = factor(age_rec))) +
  geom_bar(position = "dodge", stat = "identity") +
  labs(x = "", y = "", fill = "") +
  scale_fill_manual(labels = c("<31" ,"31-59" ,"60+"),
                      values=c("#000000", "#676767",  "darkgrey"))+
  scale_y_continuous(breaks=seq(0,0.5,0.10),limits=c(0, 0.5),labels = scales::percent) +
  theme_classic() +
  scale_x_continuous(breaks = c(1, 2, 3,4,5,6,7,8,9,10,11),
    label = c("-5", "-4", "-3","-2","-1","0","+1","+2","+3","+4","+5"))+
  theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=14,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))

youngrepresentation_age


#### Old descriptive Representation #####
subset_old_desc <- subset %>%
  group_by(age_rec, oldrep) %>%
  summarise(count = n()) %>%
  group_by(age_rec) %>%
  mutate(percent_young = count / sum(count))

# Plot 
oldrepresentation_age <- ggplot(subset_old_desc, aes(x = oldrep, y = percent_young, fill = factor(age_rec))) +
  geom_bar(position = "dodge", stat = "identity") +
  labs(x = "", y = "", fill = "") +
  scale_fill_manual(labels = c("<31" ,"31-59" ,"60+"),
                    values=c("#000000", "#676767",  "darkgrey"))+
  scale_y_continuous(breaks=seq(0,0.5,0.10),limits=c(0, 0.5),labels = scales::percent) +
  theme_classic() +
  scale_x_continuous(breaks = c(1, 2, 3,4,5,6,7,8,9,10,11),
                     label = c("-5", "-4", "-3","-2","-1","0","+1","+2","+3","+4","+5"))+
  theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=14,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))

oldrepresentation_age

#####  Descriptives Importance on Level #####

ggplot(subset, aes(x = agelocal)) +  
  geom_bar(aes(y = (..count..)/sum(..count..)), na.rm = T,color=c("#187bcd"),fill=c("#187bcd"))+
  scale_x_continuous (breaks = c(1,2,3,4,5),
                      labels=c("not at all important","rather not important", 
                               " partly/partly", 
                               "rather important","very important"))+
  labs(title = '', y = '', x='') +
  geom_text(aes(label = scales::percent(..prop.., accuracy = 0.01), y = ..prop..),
                                     stat = "count", vjust = -0.5, family="serif") +  theme_classic()+
  theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=14,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))+
  ylab("")+
  xlab("") +theme(axis.title.y=element_blank(),
                  axis.text.y=element_blank(),
                  axis.ticks.y=element_blank()) + ylim(0,0.5)

ggsave(filename="age_local_level.jpeg",
       width=10,
       height=6,
       device='jpeg',
       dpi=800)


ggplot(subset, aes(x = agestate)) +  
  geom_bar(aes(y = (..count..)/sum(..count..)), na.rm = T,color=c("#187bcd"),fill=c("#187bcd"))+
  scale_x_continuous (breaks = c(1,2,3,4,5),
                      labels=c("not at all important","rather not important", 
                               " partly/partly", 
                               "rather important","very important"))+
  labs(title = '', y = '', x='') +
  geom_text(aes(label = scales::percent(..prop.., accuracy = 0.01), y = ..prop..),
            stat = "count", vjust = -0.5, family="serif") +  theme_classic()+
  theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=14,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))+
  ylab("")+
  xlab("") +theme(axis.title.y=element_blank(),
                  axis.text.y=element_blank(),
                  axis.ticks.y=element_blank()) + ylim(0,0.5)

ggsave(filename="age_state_level.jpeg",
       width=10,
       height=6,
       device='jpeg',
       dpi=800)



ggplot(subset, aes(x = agenat)) +  
  geom_bar(aes(y = (..count..)/sum(..count..)), na.rm = T,color=c("#187bcd"),fill=c("#187bcd"))+
  scale_x_continuous (breaks = c(1,2,3,4,5),
                      labels=c("not at all important","rather not important", 
                               " partly/partly", 
                               "rather important","very important"))+
  labs(title = '', y = '', x='') +
  geom_text(aes(label = scales::percent(..prop.., accuracy = 0.01), y = ..prop..),
            stat = "count", vjust = -0.5, family="serif") +  theme_classic()+
  theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=14,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))+
  ylab("")+
  xlab("") +theme(axis.title.y=element_blank(),
                  axis.text.y=element_blank(),
                  axis.ticks.y=element_blank()) + ylim(0,0.5)

ggsave(filename="age_nationlevel.jpeg",
       width=10,
       height=6,
       device='jpeg',
       dpi=800)



##### Descriptives Age Local/State/Nation * Age #####

subset_age_local <- subset %>%
  group_by(age_rec, agelocal) %>%
  summarise(count = n()) %>%
  group_by(age_rec) %>%
  mutate(percent_local = count / sum(count))

# Plot 
localrepresentation_age <- ggplot(subset_age_local, aes(x = agelocal, y = percent_local, fill = factor(age_rec))) +
  geom_bar(position = "dodge", stat = "identity") +
  labs(x = "", y = "", fill = "") +
  scale_fill_manual(labels = c("<31" ,"31-59" ,"60+"),
                    values=c("#000000", "#676767",  "darkgrey"))+
  scale_y_continuous(breaks=seq(0,0.5,0.10),limits=c(0, 0.5),labels = scales::percent) +
  theme_classic() +
  scale_x_continuous (breaks = c(1,2,3,4,5),
                      labels=c("not at all important","rather not important", 
                               " partly/partly", 
                               "rather important","very important"))+
    theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=14,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))

localrepresentation_age

subset_age_state <- subset %>%
  group_by(age_rec, agestate) %>%
  summarise(count = n()) %>%
  group_by(age_rec) %>%
  mutate(percent_state = count / sum(count))

# Plot 
staterepresentation_age <- ggplot(subset_age_state, aes(x = agestate, y = percent_state, fill = factor(age_rec))) +
  geom_bar(position = "dodge", stat = "identity") +
  labs(x = "", y = "", fill = "") +
  scale_fill_manual(labels = c("<31" ,"31-59" ,"60+"),
                    values=c("#000000", "#676767",  "darkgrey"))+
  scale_y_continuous(breaks=seq(0,0.5,0.10),limits=c(0, 0.5),labels = scales::percent) +
  theme_classic() +
  scale_x_continuous (breaks = c(1,2,3,4,5),
                      labels=c("not at all important","rather not important", 
                               " partly/partly", 
                               "rather important","very important"))+
  theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=14,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))

staterepresentation_age


subset_age_nat <- subset %>%
  group_by(age_rec, agenat) %>%
  summarise(count = n()) %>%
  group_by(age_rec) %>%
  mutate(percent_nat = count / sum(count))

# Plot 
natrepresentation_age <- ggplot(subset_age_nat, aes(x = agenat, y = percent_nat, fill = factor(age_rec))) +
  geom_bar(position = "dodge", stat = "identity") +
  labs(x = "", y = "", fill = "") +
  scale_fill_manual(labels = c("<31" ,"31-59" ,"60+"),
                    values=c("#000000", "#676767",  "darkgrey"))+
  scale_y_continuous(breaks=seq(0,0.5,0.10),limits=c(0, 0.5),labels = scales::percent) +
  theme_classic() +
  scale_x_continuous (breaks = c(1,2,3,4,5),
                      labels=c("not at all important","rather not important", 
                               " partly/partly", 
                               "rather important","very important"))+
  theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=14,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))

natrepresentation_age

localrepresentation_age

ggsave(filename="LocalLevel_AgeRepresentation.jpeg",
       width=13,
       height=5,
       device='jpeg',
       dpi=800)

staterepresentation_age

ggsave(filename="stateLevel_AgeRepresentation.jpeg",
       width=13,
       height=5,
       device='jpeg',
       dpi=800)

natrepresentation_age

ggsave(filename="nationLevel_AgeRepresentation.jpeg",
       width=13,
       height=5,
       device='jpeg',
       dpi=800)


##### Robustness Check Age Effects #####

youngrep_b<- lm(formula = youngrep ~ age + sex + blueworker+ education + 
                       urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)

oldrep_b <- lm(formula = oldrep ~ age + sex+ blueworker + education + 
                     urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)

femalerep_b <- lm(formula = female_rep ~ age + sex + blueworker+ education + 
                        urbanrural + polint + lib_con + demsatis + representsatis, data=subset)

malerep_b <- lm(formula = male_rep ~ age  + sex + blueworker+ education + 
                      urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)

bluecollarrep_b <- lm(formula = bluecollar ~ age + sex+ blueworker + education + 
                            urbanrural + polint + lib_con + demsatis + representsatis, data=subset)


youngrep_c<- lm(formula = youngrep ~ age*age + blueworker+ education + 
                  urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)

oldrep_c <- lm(formula = oldrep ~ age*age+ blueworker + education + 
                 urbanrural  + polint + lib_con + demsatis + representsatis, data=subset)

summary(youngrep_c)
summary(oldrep_c)

#### Distribution Age Categories ###

table(subset$age_rec)
prop.table(table(subset$age_rec))


# Further Barplots #

histoyoung <- ageboxplots %>%
  filter(!is.na(youngrep)) %>%
  count(youngrep) %>%
  mutate(percentage = n / sum(n) * 100) %>%
  ggplot(aes(x = as.factor(youngrep), y = percentage)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  geom_text(aes(label = round(percentage, 2)), vjust = -0.5, family="serif") + 
  labs(x = "", y = "Percentage (%)", 
       title = "Barplot of Younger Representative (in %)") +
  theme_classic()+ ylim(0,40)+
  scale_x_discrete(breaks = c(1, 2, 3,4,5,6,7,8,9,10,11),
                     label = c("-5", "-4", "-3","-2","-1",
                               "0","+1","+2","+3","+4","+5"))+
  theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=16,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))


histoold<- ageboxplots %>%
  filter(!is.na(oldrep)) %>%
  count(oldrep) %>%
  mutate(percentage = n / sum(n) * 100) %>%
  ggplot(aes(x = as.factor(oldrep), y = percentage)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  geom_text(aes(label = round(percentage, 2)), vjust = -0.5, family="serif") + 
  labs(x = "", y = "Percentage (%)", 
       title = "Barplot of Older  Representative (in %)") +
  theme_classic()+ ylim(0,40)+
  scale_x_discrete(breaks = c(1, 2, 3,4,5,6,7,8,9,10,11),
                   label = c("-5", "-4", "-3","-2","-1","0","+1","+2",
                             "+3","+4","+5"))+
  theme(text=element_text(size=14, family="serif",color = "black"),legend.text = element_text(color = "black", size = 14),
        axis.text=element_text(size=14, family="serif",color = "black"),
        axis.title=element_text(size=16,family="serif",color = "black"),
        axis.text.x   = element_text(family = "serif", size=14,color = "black"))

ggarrange(histoyoung, histoold)

ggsave(filename="histos.jpeg",
       width=12,
       height=4,
       device='jpeg',
       dpi=800)



##### Summary Statistics ######

library(psych)

print <- describe(subset)

library(writexl)

write_xlsx(print, "print.xlsx")

t.test(subset$age)$conf.int
t.test(subset$representsatis)$conf.int
t.test(subset$youngrep)$conf.int
t.test(subset$oldrep)$conf.int
t.test(subset$female_rep)$conf.int
t.test(subset$male_rep)$conf.int
t.test(subset$agelocal)$conf.int
t.test(subset$agestate)$conf.int
t.test(subset$agenat)$conf.int

round(prop.table(table(subset$age_rec))*100,2)
round(prop.table(table(subset$sex))*100,2)
round(prop.table(table(subset$urbanrural))*100,2)
round(prop.table(table(subset$education))*100,2)
round(prop.table(table(subset$polint))*100,2)
round(prop.table(table(subset$demsatis))*100,2)
round(prop.table(table(subset$blueworker))*100,2)

